package com.noble.glamour.ui.user.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.model.impl.UnreadMgrImpl;
import com.noble.glamour.third.recyclerholder.BaseRecyclerViewAdapter;
import com.noble.glamour.third.recyclerholder.BaseRecyclerViewHolder;
import com.noble.glamour.ui.user.fragment.bean.UserAuth;
import com.noble.mail.unread.BadgeView;

import static com.noble.glamour.module.logic.application.App.context;

/**
 * 个人中心布局adapter
 * Created by Su on 2017/3/28.
 */

public class UserAuthAdapter extends BaseRecyclerViewAdapter {
    @Override
    public int[] getItemLayouts() {
        return new int[]{R.layout.p1_user_fragment_list_item};
    }

    @Override
    public void onBindRecycleViewHolder(BaseRecyclerViewHolder viewHolder, int position) {
        UserAuth userAuth = (UserAuth) getItem(position);

        View item_view = viewHolder.findViewById(R.id.item_view);
        ImageView item_icon = viewHolder.findViewById(R.id.item_icon);
        TextView item_text = viewHolder.findViewById(R.id.item_text);
        TextView item_hint = viewHolder.findViewById(R.id.item_hint);
        BadgeView item_badge = viewHolder.findViewById(R.id.item_badge);
        View divider_line = viewHolder.findViewById(R.id.divider_line);
        View divider_view = viewHolder.findViewById(R.id.divider_view);

        //区块划分
        int select = getListItemSelection(userAuth, position);
        if (select == 1) {
            divider_line.setVisibility(View.GONE);
            divider_view.setVisibility(View.VISIBLE);
        } else if (select == -1) {
            divider_line.setVisibility(View.VISIBLE);
            divider_view.setVisibility(View.GONE);
        } else {
            divider_line.setVisibility(View.GONE);
            divider_view.setVisibility(View.GONE);
        }

        //角标提示
        item_hint.setVisibility(View.GONE);

        if (!userAuth.isShow()) {
            divider_line.setVisibility(View.GONE);
            item_view.setVisibility(View.GONE);
            return;
        }
        item_view.setVisibility(View.VISIBLE);
        item_icon.setBackgroundResource(userAuth.getRes());
        item_text.setText(userAuth.getName());

        // 各条目展示逻辑
//        if (userAuth.getId() == CenterItemID.i_Center_item_2) {  // 谁关注我
//            ModuleMgr.getUnreadMgr().registerBadge(item_badge, true, UnreadMgrImpl.FOLLOW_ME);
//        }

        if (userAuth.getId() == CenterItemID.i_Center_item_2) {  // 我的钱包
            String num = String.valueOf(ModuleMgr.getCenterMgr().getMyInfo().getRedbagsum() / 100f);
            item_hint.setVisibility(View.VISIBLE);
            item_hint.setText(context.getString(R.string.user_info_redbag_num, num));

            ModuleMgr.getUnreadMgr().registerBadge(item_badge, true, UnreadMgrImpl.MY_WALLET);
        }

//        if (userAuth.getId() == CenterItemID.i_Center_item_4) {  // 我要赚红包
//            item_hint.setVisibility(View.VISIBLE);
//            item_hint.setText(context.getString(R.string.user_info_earn_redbag));
//        }

//        if (userAuth.getId() == CenterItemID.i_Center_item_6) {  // 我的Y币
//            item_hint.setVisibility(View.VISIBLE);
//            UserDetail detail = ModuleMgr.getCenterMgr().getMyInfo();
//            if(detail.isB()){
//                item_hint.setText(String.valueOf(detail.getKeyCount()));
//            }else{
//                item_hint.setText(String.valueOf(detail.getYcoin()));
//            }
//        }

        if (userAuth.getId() == CenterItemID.i_Center_item_3) {  // 我的钻石
            item_hint.setVisibility(View.VISIBLE);
            item_hint.setText(String.valueOf(ModuleMgr.getCenterMgr().getMyInfo().getDiamand()));
        }

//        if (userAuth.getId() == CenterItemID.i_Center_item_5) {//自动回复
//            if (PSP.getInstance().getBoolean(
//                    ModuleMgr.getCommonMgr().getPrivateKey(FinalKey.ISSHOWAUTOREPLYPOINT_MYHOME), true)
//                    && !ModuleMgr.getCenterMgr().getMyInfo().isMan()) {
//                item_hint.setVisibility(View.VISIBLE);
//                item_hint.setText("NEW");
//                item_hint.setTextSize(9);
//                item_hint.setTextColor(ContextCompat.getColor(item_hint.getContext(), R.color.white));
//                item_hint.setBackgroundResource(R.drawable.bg_my_autoreply);
//
//                RelativeLayout.LayoutParams item_hint_lp = (RelativeLayout.LayoutParams) item_hint.getLayoutParams();
//                item_hint_lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
//                item_hint_lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
//                item_hint.setLayoutParams(item_hint_lp);
//
//                item_hint.setPadding((int) item_hint.getResources().getDimension(R.dimen.px15_dp),
//                        (int) item_hint.getResources().getDimension(R.dimen.px10_dp),
//                        (int) item_hint.getResources().getDimension(R.dimen.px15_dp),
//                        (int) item_hint.getResources().getDimension(R.dimen.px10_dp));
//            } else {
//                item_hint.setVisibility(View.GONE);
//            }
//        }
    }

    @Override
    public int getRecycleViewItemType(int position) {
        return 0;
    }

    /**
     * 处理当前item的level
     */
    private int getListItemSelection(UserAuth userAuth, int position) {
        if (position + 1 < getItemCount()) {
            if (userAuth.getLevel() != ((UserAuth) getItem(position + 1)).getLevel()) {
                return 1;//不同区块
            }
        } else {
            return 0;//集合末尾
        }
        return -1;//相同区块
    }
}
